*******************************************
** S-RLS Panel Phone Survey **
** Data Cleaning File      **                                    
******************************************
clear all
set more off
set maxvar 10000
//ssc install missings 
	
use "${dir}/raw_public/PS_deidentified_public.dta", clear  


*****************************CLEANING**************************
**Changing all indicator variables to 0,1
quietly ds , has(type numeric)
	foreach var in `r(varlist)' {
		quietly count if !inlist(`var',.,1,2)
		if `r(N)'==0 {
			assert inlist(`var',.,1,2)==1
			local label1 :  label  `var' 1
			local label2 :  label  `var' 2
			if "`label1'"=="Yes" & "`label2'"=="No" {
				replace `var' = 0 if `var'==2 
				assert inlist(`var',.,1,0)
				lab define `var' 0 "No" 1 "Yes", replace 
				lab values `var' `var'
			} 
		}
	else {
		quietly count if !inlist(`var',.,1,2,-99)
		if `r(N)'==0 {
			assert inlist(`var',.,1,2,-99)==1
			local label1 :  label  `var' 1
			local label2 :  label  `var' 2
			local label9 : label `var' -99
			if "`label1'"=="Yes" & "`label2'"=="No" {
				replace `var' = 0 if `var'==2 
				assert inlist(`var',.,1,0,-99)
				lab define `var' 0 "No" 1 "Yes" -99 "`label9'", replace 
				lab values `var' `var'
				tab `var',mi nolab
			}
		}
	}
}

**Dropping numeric variables with 0 obs
foreach var of varlist _all {
	capture assert mi(`var')
	if !_rc {
		drop `var'
	}
}

**Remove hidden spaces from string variables
ds, has (type string)
local strvars `r(varlist)' 
foreach var of varlist `r(varlist)' {
	replace `var' = strtrim(`var')
}

**Droping all variables with only "." and all string variables with 0 obs
 missings dropvars, force
 
**Drop redundant variable
assert q601a== q601a_null
drop  q601a_null

 
******************************DATA ORGANIZATION**************************
rename id_n id 

******Renaming to match survey doc****** 

rename q517 q512
rename q518 q513
rename q519 q514
rename q519a q514a

rename q6_1_05a q6_1_04a

rename q6_2_05 q6_2_3
rename q6_2_05a q6_2_3a
rename q6_2_05a_currency q6_2_3a_currency
rename q6_2_05a_currency_other q6_2_3a_currency_other

rename q6_2_08 q6_2_4

rename q6_2_09  q6_2_5
rename q6_2_09b q6_2_5a
rename q6_2_09b_currency q6_2_5b_currency

rename q6_2_12 q6_2_6
rename q6_2_12_1 q6_2_6_1
rename q6_2_12_2 q6_2_6_2
rename q6_2_12_3 q6_2_6_3
rename q6_2_12_9 q6_2_6_4
rename q6_2_12_4 q6_2_6_5

rename q6_2_03 q6_2_7
rename q6_2_03a q6_2_7a

rename q6_2_06  q6_2_8
rename q6_2_06a q6_2_8a 
rename q6_2_06a_currency q6_2_8a_currency
rename q6_2_06a_currency_other q6_2_8a_currency_other

rename q6_2_10  q6_2_9 
rename q6_2_10b q6_2_9a
rename q6_2_10b_currency q6_2_9a_currency
 
rename q6_2_13 q6_2_10
rename q6_2_13_1 q6_2_10_1
rename q6_2_13_2 q6_2_10_2
rename q6_2_13_3 q6_2_10_3
rename q6_2_13_9 q6_2_10_9
rename q6_2_13_4 q6_2_10_4

rename q6_2_11  q6_2_13 
rename q6_2_11b q6_2_13a
rename q6_2_11b_currency q6_2_13a_currency

rename q6_2_04 q6_2_11
rename q6_2_04a q6_2_11a 
 
rename q6_2_07  q6_2_12 
rename q6_2_07a  q6_2_12a 
rename q6_2_07a_currency q6_2_12a_currency
rename q6_2_07a_currency_other q6_2_12a_currency_other

rename q709 q702

rename q710a q703a
rename q710b q703b
rename q710b_other q703b_other

rename q304b_domain_other_C1 q303b_domain_other_C1
rename q305 q304
rename q305a q304a
rename q306a_a q304a_a
rename q306a_a_other q304a_a_other
rename q306a_a_other_C1 q304a_a_other_C1
rename q306c_rel_other_C1 q304c_rel_other_C1
rename q306c_rel q304c_rel
rename q306c_rel_other q304c_rel_other
rename q307 q305
rename q307_prob q305_prob
rename q307a q305a
rename q307a_a q305a_a
rename q307a_a_other q305a_a_other
rename q307c q305c
rename q307a_a_other_C1 q305a_a_other_C1
rename q308 q306
rename q308_prob q306_prob
rename q308c q306c
rename q309 q307
rename q309a1 q307a1
rename q309a_domain q307a_domain
rename q309a_domain_other q307a_domain_other
rename q309a_domain_other_C1 q307a_domain_other_C1
rename q310 q308


rename q312 q310
rename q312a q310a
rename q314 q312
rename q316 q314
rename q317 q315
rename q320 q316
rename q320_prob q316_prob
rename q320a q316a
rename q322 q317
rename q322_prob q317_prob
rename q322a q317a

rename q332 q323
rename q332_prob q323_prob
rename q332a q323a
rename q325 q318
rename q327 q320
rename q328 q321
rename q331 q322
rename q331_prob q322_prob
rename q331a q322a
rename q335 q324

* List all variables in the dataset
ds, has(type numeric) 

* Loop through each numeric variable and replace 710 with 703 
foreach var of varlist _all {
    * Check if variable name contains "710"
    if strpos("`var'", "710") > 0 {
        * Rename the variable by replacing "710" with "703"
        local newvarname : subinstr local var "710" "703", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 711 with 704
foreach var of varlist _all {
    * Check if variable name contains "711"
    if strpos("`var'", "711") > 0 {
        * Rename the variable by replacing "711" with "704"
        local newvarname : subinstr local var "711" "704", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 713 with 706
foreach var of varlist _all {
    * Check if variable name contains "713"
    if strpos("`var'", "713") > 0 {
        * Rename the variable by replacing "713" with "706"
        local newvarname : subinstr local var "713" "706", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 714 with 707
foreach var of varlist _all {
    * Check if variable name contains "714"
    if strpos("`var'", "714") > 0 {
        * Rename the variable by replacing "714" with "707"
        local newvarname : subinstr local var "714" "707", all
        rename `var' `newvarname'
    }
}

rename q715 q708 
rename q717 q710 

* Loop through each numeric variable and replace 718 with 711
foreach var of varlist _all {
    * Check if variable name contains "718"
    if strpos("`var'", "718") > 0 {
        * Rename the variable by replacing "718" with "711"
        local newvarname : subinstr local var "718" "711", all
        rename `var' `newvarname'
    }
}

rename q907 q901

* Loop through each numeric variable and replace 908 with 902
foreach var of varlist _all {
    * Check if variable name contains "908"
    if strpos("`var'", "908") > 0 {
        * Rename the variable by replacing "908" with "902"
        local newvarname : subinstr local var "908" "902", all
        rename `var' `newvarname'
    }
}


**  Loop through each numeric variable and replace 311 with 309 
foreach var of varlist _all {
    * Check if variable name contains "311"
    if strpos("`var'", "311") > 0 {
        * Rename the variable by replacing "311" with "309"
        local newvarname : subinstr local var "311" "309", all
        rename `var' `newvarname'
    }
}


**Loop through each numeric variable and replace 336 with 325
foreach var of varlist _all {
    * Check if variable name contains "336"
    if strpos("`var'", "336") > 0 {
        * Rename the variable by replacing "336" with "325"
        local newvarname : subinstr local var "336" "325", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 913 with 903
foreach var of varlist _all {
    * Check if variable name contains "913"
    if strpos("`var'", "913") > 0 {
        * Rename the variable by replacing "913" with "903"
        local newvarname : subinstr local var "913" "903", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 914 with 904
foreach var of varlist _all {
    * Check if variable name contains "914"
    if strpos("`var'", "914") > 0 {
        * Rename the variable by replacing "914" with "904"
        local newvarname : subinstr local var "914" "904", all
        rename `var' `newvarname'
    }
}

* Loop through each numeric variable and replace 915 with 905
foreach var of varlist _all {
    * Check if variable name contains "915"
    if strpos("`var'", "915") > 0 {
        * Rename the variable by replacing "915" with "905"
        local newvarname : subinstr local var "915" "905", all
        rename `var' `newvarname'
    }
}


save "${dir}/intermediate_public/PS_deidentified_public_clean.dta", replace
